package de.hska.util;

import static de.hska.util.EjbConstants.HASH_ALGORITHM;
import static de.hska.util.EjbConstants.HASH_CHARSET;
import static de.hska.util.EjbConstants.HASH_ENCODING;
import static org.jboss.security.Util.createPasswordHash;

/**
 * In Anlehnung an org.jboss.test.PasswordHasher von Scott Stark
 */
public class PasswordUtil {
	final static String USAGE = "Usage: java " + PasswordUtil.class.getName() + " <password>";

	/**
	 */
	public static void main(String[] args) {
		if ((args.length != 1) || args[0].startsWith("-h"))
			throw new IllegalStateException(USAGE);

		String password = args[0];
		final String verschluesselt = verschluesseln(password);
		
		System.out.println("Zu verschluesselndes Password: >>>" + password + "<<<");
		System.out.println("Verschluesseltes Password:     >>>" + verschluesselt + "<<<");
		System.out.println("");
		System.out.println("Hash-Algorithmus: " + HASH_ALGORITHM);
		System.out.println("Hash-Encoding:    " + HASH_ENCODING);
		System.out.println("Hash-Charset:     " + HASH_CHARSET);
	}

	/**
	 */
	// TODO bei Portierung: JBoss-spezifische Funktionalitaet ersetzen 
	public static String verschluesseln(String password) {
		final String passwordHash = createPasswordHash(HASH_ALGORITHM, HASH_ENCODING, HASH_CHARSET, null, password);
		return passwordHash;
	}
}
